package sg.edu.nus.iss.billing.vo.subscription;

import java.util.Date;
import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;
import sg.edu.nus.iss.billing.vo.common.IDbSerialisable;

/**
 * @author Chee Ann
 *
 */
public class SubscriptionVO implements IDbSerialisable {

	protected String subsId;
	protected String accNo;
	protected String name;
	protected Date startDate;
	protected Date endDate;
	protected double monthlySuscription;
	protected String status;
	protected String description;
	
	public SubscriptionVO() {
		setAccNo(Util.Default_NULL);
		setSubsId(Util.Default_NULL);
		setName(Util.Default_NULL);
		setStartDate(Util.getNullDate());
		setEndDate(Util.getNullDate());
		setMonthlySuscription(0);
		setStatus(Util.Default_NULL);
		setDescription(Util.Default_NULL);
	}
	
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the subsId
	 */
	public String getSubsId() {
		return subsId;
	}
	
	/**
	 * @param subsId the subsId to set
	 */
	public void setSubsId(String subsId) {
		this.subsId = subsId;
	}
	
	/**
	 * @return the accNo
	 */
	public String getAccNo() {
		return accNo;
	}
	
	/**
	 * @param accNo the accNo to set
	 */
	public void setAccNo(String accNo) {
		this.accNo = accNo;
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}
	
	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}
	
	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	/**
	 * @return the monthlySuscription
	 */
	public double getMonthlySuscription() {
		return monthlySuscription;
	}
	
	/**
	 * @param monthlySuscription the monthlySuscription to set
	 */
	public void setMonthlySuscription(double monthlySuscription) {
		this.monthlySuscription = monthlySuscription;
	}
	
	public String getStatus() {
		return status;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}
	
	@Override
	public void setParameters(String serialisedEntry)
			throws InvalidParamsException {
		String[] tokens =  serialisedEntry.split(java.util.regex.Pattern.quote(DbUtil.DELIMITER));
		if (tokens.length <8 ) {
			throw new InvalidParamsException("Expected 8 or more parameters, received " + tokens.length);
		} else {			
			setAccNo(tokens[0]);
			setSubsId(tokens[1]);
			setName(tokens[2]);
			setStartDate(Util.getDateFromString(tokens[3]));
			setEndDate(Util.getDateFromString(tokens[4]));
			setMonthlySuscription(Double.parseDouble(tokens[5]));
			setStatus(tokens[6]);
			setDescription(tokens[7]);
		}
	}

	@Override
	public String serialised() {
		StringBuilder sb = new StringBuilder();
		sb.append(getAccNo());
		sb.append(DbUtil.DELIMITER);
		sb.append(getSubsId());
		sb.append(DbUtil.DELIMITER);
		sb.append(getName());
		sb.append(DbUtil.DELIMITER);
		sb.append(Util.getFormattedDate(getStartDate()));
		sb.append(DbUtil.DELIMITER);
		sb.append(Util.getFormattedDate(getEndDate()));
		sb.append(DbUtil.DELIMITER);
		sb.append(getMonthlySuscription());
		sb.append(DbUtil.DELIMITER);
		sb.append(getStatus());
		sb.append(DbUtil.DELIMITER);
		sb.append(getDescription());
		return sb.toString();
	}
}
